<?php
	// No direct access
	defined('_JEXEC') or die;
    jimport( 'joomla.application.component.view');
    class VolunteerViewEditfile extends JView
    {
		function display($tpl = null)
		{
			$fileId = JRequest::getVar('fileid');
		
			$model = $this->getModel('volunteer'); //get default model (volunteer)
			
			$this->assignRef( 'locations', $model->getLocations() );
			$this->assignRef( 'timespans', $model->getTimespans() );
			$this->assignRef( 'industries', $model->getIndustries() );
			$this->assignRef( 'keywordList', $model->getKeywords() );
									
			$app = JFactory::getApplication();
			$data = $app->getUserState('volunteer.editfile.form.data');
			if($data['audiofile'] == null)
			{
				$file = $model->getFile($fileId);
				$keywords = $model->getFileKeywords($fileId);
				$newmp3 = 'current';
			}
			else
			{				
				$file = $data['audiofile'];
				
				if($file->id != $fileId)
				{
					$file = $model->getFile($fileId);
					$keywords = $model->getFileKeywords($fileId);
					$newmp3 = 'current';
				}
				else
				{					
					$keywords = $data['keywords'];
					$newmp3 = $data['newmp3'];
				}
			}
			
			// echo $newmp3;
			
			$this->assignRef( 'file', $file);
			$this->assignRef( 'keywords', $this->joinKeywords($keywords));
			$this->assignRef( 'newmp3', $newmp3);
			$this->assignRef( 'linkToFile', $data['linkToFile']);
		
			parent::display($tpl); //display defaul template
		}
		
		private function joinKeywords($keywordList)
		{
			$keywordString = '';
			
			foreach($keywordList as $keyword)
			{
				$keywordString .= $keyword->keyword . ',';
			}
			
			if(strlen($keywordString) > 0)
				$keywordString = substr($keywordString, 0, strlen($keywordString) - 1);
				
			return $keywordString;
		}
    }